package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Gys;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GysForm extends BasePage implements Serializable {
    private GenericManager<Gys, Long> gysManager;
    private Gys gys = new Gys();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setGysManager(GenericManager<Gys, Long> gysManager) {
        this.gysManager = gysManager;
    }

    public Gys getGys() {
        return gys;
    }

    public void setGys(Gys gys) {
        this.gys = gys;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        gysManager.remove(gys.getId());
        addMessage("gys.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            gys = gysManager.get(id);
        } else {
            gys = new Gys();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (gys.getId() == null || gys.getId() == 0);
        // set the related jbqk as a foreign key.
        gys.setJbqk( jbqk );
        gysManager.save(gys);

        String key = (isNew) ? "gys.added" : "gys.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 